package test_consumo;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class Llamado_remoto {
	static final String[] SESION = { "0", "1", "2", "3" };
	static String[] asUsuarioSAP = { "", "", "", "" };
	static boolean bComprimido = false;
	private static boolean bIsConexionSocket = false;
	static int iPuertoSK = 0;
	static ServerSocket serverSocket = null;
	public static String[] getDatosConexion()
	{
		String[] strings = new String[5];
		strings[0] = asUsuarioSAP[0];
		strings[1] = "TRON2000";//sClaveSAP;
		strings[2] = "TRON2000";//sUsuarioORA;
		strings[3] = "TRON2000";//sClaveORA;
		strings[4] = "jdbc:oracle:thin:@";//sCadenaConexion;
		return strings;
	}
	private static Long getTimeOutUsuario()
	{
		String string = "100000";
		return new Long(string);
	}
	public static int getConexionActiva()
	{
		return 0;
	}
	Llamado_remoto(){
		asUsuarioSAP[0] = "TRON2000" + "$3";
		asUsuarioSAP[1] = "TRON2000" + "$3";
		asUsuarioSAP[2] = "TRON2000" + "$3";
		//Object[] objects=null;
		Object[] objects = { SESION[getConexionActiva()], "CNX",getDatosConexion(), getTimeOutUsuario() };
		Object object = null;
		try {
			URL url = new URL("http://localhost:8080/SERVIDOR_TOMCAT/SVGenerador");
			HttpURLConnection httpurlconnection = (HttpURLConnection)url.openConnection();
			httpurlconnection.setDoOutput(true);
			httpurlconnection.setDoInput(true);
			httpurlconnection.setUseCaches(false);
			httpurlconnection.setRequestProperty("Content-Type","octec");
			
			OutputStream outputstream = httpurlconnection.getOutputStream();
			
			ObjectOutputStream objectoutputstream;
			
			objectoutputstream = new ObjectOutputStream(outputstream);
			System.out.println(objectoutputstream);
			objectoutputstream.writeObject(objects);
			objectoutputstream.flush();
			objectoutputstream.close();
			
			
			ObjectInputStream objectinputstream;
			if (httpurlconnection.getContentType() != null
				&& httpurlconnection.getContentType().equals("compress"))
			{
				GZIPInputStream gzipinputstream
				= new GZIPInputStream(httpurlconnection
							  .getInputStream());
				objectinputstream = new ObjectInputStream(gzipinputstream);
			}
			else
				objectinputstream= new ObjectInputStream(httpurlconnection
								.getInputStream());
			
			
			object = objectinputstream.readObject();
			objectinputstream.close();
			httpurlconnection.getInputStream().close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Llamado_remoto();

	}
	public static boolean isComprimido()
	{
		return bComprimido;
	}

	public static void setComprimido(boolean bool)
	{
		bComprimido = bool;
	}
	public static String getIdSesion()
	{
		return SESION[getConexionActiva()];
	}
	public static boolean isConexionSocket()
	{
		return bIsConexionSocket;
	}

	public static void setConexionSocket(boolean bool)
	{
		bIsConexionSocket = bool;
	}
	private static ServerSocket getSSock(int i)
	{
		ServerSocket serversocket;
		try
		{
			ServerSocket serversocket_33_ = new ServerSocket(i + 1);
			serversocket_33_.setSoTimeout(0);
			serversocket = serversocket_33_;
		}
		catch (Exception exception)
		{
			return null;
		}
		return serversocket;
	}
	private static boolean enviarObjetoSocket(Object object, String string,
			  int i)
	{
	boolean bool;
	try
	{
		Socket socket = new Socket(string, i);
		OutputStream outputstream = socket.getOutputStream();
		GZIPOutputStream gzipoutputstream
		= new GZIPOutputStream(outputstream);
		ObjectOutputStream objectoutputstream
		= new ObjectOutputStream(gzipoutputstream);
		objectoutputstream.writeObject(object);
		objectoutputstream.flush();
		objectoutputstream.close();
		bool = true;
	}
	catch (Exception exception)
	{
	return true;
	}
	return bool;
	}
	private static Object recibirObjetoSocket()
	{
		do
		{
			Object object;
			try
			{
				Socket socket = serverSocket.accept();
				if (socket == null)
					break;
				InputStream inputstream = socket.getInputStream();
				GZIPInputStream gzipinputstream
					= new GZIPInputStream(inputstream);
				ObjectInputStream objectinputstream
					= new ObjectInputStream(gzipinputstream);
				Object object_34_ = objectinputstream.readObject();
				object = object_34_;
			}
			catch (Exception exception)
			{
				break;
			}
			return object;
		} while (false);
		return null;
	}
	public static Object ejecutarMetodoRemotoSK
	(Object[] objects, String string) throws RemoteException
	{
		Object[] objects_35_ = { string, objects };
		if (enviarObjetoSocket(objects_35_, "localhost:8080",
					   iPuertoSK))
			return recibirObjetoSocket();
		return null;
	}
	public static Object ejecutarMetodoRemoto
	(Object[] objects, String string) throws RemoteException
	{

		objects[0] = getIdSesion();
		Object object = null;
		long l = System.currentTimeMillis();
		int i = 0;
		if (isConexionSocket() && !getIdSesion().equals("0"))
			object = ejecutarMetodoRemotoSK(objects, string);
		else
		{
			Object object_36_ = null;
			Object object_37_ = null;
			Object object_38_ = null;
			Object object_39_ = null;
			Object object_40_ = null;
			setComprimido(i > 120);
			try
			{
				URL url = new URL(string);
				HttpURLConnection httpurlconnection
					= (HttpURLConnection)url.openConnection();
				httpurlconnection.setDoOutput(true);
				httpurlconnection.setDoInput(true);
				httpurlconnection.setUseCaches(false);
				if (isComprimido())
					httpurlconnection.setRequestProperty("Content-Type",
									 "compress");
				else
					httpurlconnection.setRequestProperty("Content-Type",
									 "octec");
				OutputStream outputstream
					= httpurlconnection.getOutputStream();
				ObjectOutputStream objectoutputstream;
				if (isComprimido())
				{
					long l_41_ = System.currentTimeMillis();
					GZIPOutputStream gzipoutputstream
					= new GZIPOutputStream(outputstream);
					objectoutputstream
					= new ObjectOutputStream(gzipoutputstream);
				}
				else
					objectoutputstream = new ObjectOutputStream(outputstream);

				objectoutputstream.writeObject(objects);
				objectoutputstream.flush();
				objectoutputstream.close();

				ObjectInputStream objectinputstream;
				if (httpurlconnection.getContentType() != null
					&& httpurlconnection.getContentType().equals("compress"))
				{
					GZIPInputStream gzipinputstream
					= new GZIPInputStream(httpurlconnection
								  .getInputStream());
					objectinputstream = new ObjectInputStream(gzipinputstream);
				}
				else
					objectinputstream
					= new ObjectInputStream(httpurlconnection
									.getInputStream());

				object = objectinputstream.readObject();
				objectinputstream.close();
				httpurlconnection.getInputStream().close();

			}
			catch (IOException ioexception)
			{
				return null;
			}
			catch (ClassNotFoundException classnotfoundexception)
			{
				return null;
			}
		}
		if (object == null)
			object = new Exception("Error desconocido");
		if (object instanceof ClassCastException)
		{
			throw new RemoteException("Error en metodo remoto ClassCastException: [>]");
		}
		return object;
	}

}
